#!/usr/bin/env bash
# Copyright (C) 2023 - 2030 Teddysun <vm@vmosc.com>


#按现有物理内存的1.5倍增加虚拟内存
function SetSwap(){
    local swap=/home/swap
    local MemTotal=$(awk '($1 == "MemTotal:"){print $2}' /proc/meminfo)
    local MemSwap=$((MemTotal+MemTotal/2))
    dd if=/dev/zero of=${swap} bs=1024 count=${MemSwap}  
    mkswap ${swap};swapon ${swap};chmod 600 ${swap}   
    echo "${swap} swap swap defaults 0 0" >> /etc/fstab 
    local SwpPiness=$(cat /proc/sys/vm/swappiness)
    if [ "$SwpPiness" -lt "60" ];then 
        sysctl vm.swappiness=60  
        echo "vm.swappiness = 60" >> /etc/sysctl.conf
    fi
}

#安装Docker
function InstDocker(){
    curl -fsSL https://get.docker.com -o get-docker.sh
    sudo sh get-docker.sh
}

#配置Docker加速
function ConfDocker(){
    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://przs4wj2.mirror.aliyuncs.com",
    "https://hub.daocloud.io",
    "https://docker.m.daocloud.io",
    "https://dockerproxy.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://docker.nju.edu.cn"
  ]
}
EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker
    #sudo apt-get install docker-compose-plugin
}

#安装Caddy
function InstCaddy(){
    sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
    sudo apt install -y caddy
}

#Docker安装memos博客
function DockerMemos(){
    mkdir -p /usr/docker/memos && cd /usr/docker/memos/
    sudo tee /usr/docker/memos/compose.yml <<-'EOF'
services:  
  memos:
    restart: unless-stopped
    image: neosmemo/memos:stable
    container_name: memos
    user: root
    ports:
      - 2800:5230
    volumes:
      - .memos/:/var/opt/memos
EOF
    docker compose up -d
}

#memos代理配置
function wwwCaddy(){
    sudo tee /etc/caddy/Caddyfile <<-'EOF'
{
    email wangtl2001@163.com
}
www.vmosc.com {
    reverse_proxy :2800
}
EOF
    sudo systemctl reload caddy
}

#Docker安装Gitea仓库
function DockerGitea(){
    mkdir -p /usr/docker/gitea && cd /usr/docker/gitea/
    mkdir -p /usr/docker/gitea/{data,config}
    sudo tee /usr/docker/gitea/compose.yml <<-'EOF'
services:
  server:
    image: docker.io/gitea/gitea:1.22.6-rootless
    restart: always
    volumes:
      - ./data:/var/lib/gitea
      - ./config:/etc/gitea
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "3000:3000"
      - "2222:2222"
EOF
    sudo chown 1000:1000 config/ data/
    docker compose up -d
}

#Gitea代理配置
function GitCaddy(){
    sudo tee /etc/caddy/Caddyfile <<-'EOF'
{
    email wangtl2001@163.com
}
www.vmosc.com {
    reverse_proxy :2800
}
git.vmosc.com {
    reverse_proxy :3000
}
EOF
    sudo systemctl reload caddy
}

#Docker安装drone-runner
function DockerDrone(){
    mkdir -p /usr/docker/drone && cd /usr/docker/drone/
    sudo tee /usr/docker/drone/compose.yml <<-'EOF'
services:
  drone:
    restart: always
    image: drone/drone:latest
    container_name: drone
    ports:
      - 3200:80
    volumes:
      - ./data/drone:/var/lib/drone/
      - ./data/data:/data/
    environment:
      - DRONE_SERVER_HOST=dr.vmosc.com
      - DRONE_SERVER_PROTO=https
      - DRONE_RPC_SECRET=a04cc51ed713173925f7efe48cac42c4
      - DRONE_GITEA_CLIENT_ID=5deff0f7-322e-40a1-8a78-b3819f74ef3c
      - DRONE_GITEA_CLIENT_SECRET=gto_r4i26daemq755mpxe37kssn5bqcab64w2pf7dyxccuhkq6qzymfq
      - DRONE_GITEA_SERVER=https://git.vmosc.com
  runner:
    restart: always
    image: drone/drone-runner-docker:latest
    container_name: runner
    ports:
      - 3300:3000
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - DRONE_RPC_PROTO=https
      - DRONE_RPC_HOST=drone
      - DRONE_RPC_SECRET=a04cc51ed713173925f7efe48cac42c4
    depends_on:
      - drone
EOF
    docker compose up -d  
}

#Drone代理配置
function DrCaddy(){
    sudo tee /etc/caddy/Caddyfile <<-'EOF'
{
    email wangtl2001@163.com
}
www.vmosc.com {
    reverse_proxy :2800
}
git.vmosc.com {
    reverse_proxy :3000
}
dr.vmosc.com {
    reverse_proxy :3200
}
EOF
    sudo systemctl reload caddy
}
#安装illa-builder
function Dockerilla(){
    mkdir -p /usr/docker/illa && cd /usr/docker/illa/
    mkdir -p /usr/docker/illa/{database,drive}
    sudo tee /usr/docker/illa/compose.yml <<-'EOF'
services:  
  illa:
    restart: always
    image: illasoft/illa-builder:latest
    container_name: illa
    ports:
      - 3100:2022
    volumes:
      - ./database:/opt/illa/database
      - ./drive:/opt/illa/drive
EOF
    docker compose up -d
}

function ilCaddy(){
    sudo tee /etc/caddy/Caddyfile <<-'EOF'
{
    email wangtl2001@163.com
}
www.vmosc.com {
    reverse_proxy :2800
}
git.vmosc.com {
    reverse_proxy :3000
}
dr.vmosc.com {
    reverse_proxy :3200
}
il.vmosc.com {
    reverse_proxy :3100
}
EOF
    sudo systemctl reload caddy
}

#安装nocobase
function DockerNoco(){
    mkdir -p /usr/docker/noco && cd /usr/docker/noco/
    sudo tee /usr/docker/noco/compose.yml <<-'EOF'
networks:
  nocobase:
    driver: bridge
services:
  app:
    image: registry.cn-shanghai.aliyuncs.com/nocobase/nocobase:latest
    restart: always
    networks:
      - nocobase
    depends_on:
      - postgres
    environment:
      - APP_KEY=your-secret-key
      - DB_DIALECT=postgres
      - DB_HOST=postgres
      - DB_DATABASE=nocobase
      - DB_USER=nocobase
      - DB_PASSWORD=nocobase
      - TZ=Asia/Shanghai
      - NOCOBASE_PKG_USERNAME=
      - NOCOBASE_PKG_PASSWORD=

    volumes:
      - ./storage:/app/nocobase/storage
    ports:
      - '2900:80'
  postgres:
    image: registry.cn-shanghai.aliyuncs.com/nocobase/postgres:16
    restart: always
    command: postgres -c wal_level=logical
    environment:
      POSTGRES_USER: nocobase
      POSTGRES_DB: nocobase
      POSTGRES_PASSWORD: nocobase
    volumes:
      - ./storage/db/postgres:/var/lib/postgresql/data
    networks:
      - nocobase
EOF
    docker compose up -d
}

function nocoCaddy(){
    sudo tee /etc/caddy/Caddyfile <<-'EOF'
{
    email wangtl2001@163.com
}
www.vmosc.com {
    reverse_proxy :2800
}
git.vmosc.com {
    reverse_proxy :3000
}
dr.vmosc.com {
    reverse_proxy :3200
}
il.vmosc.com {
    reverse_proxy :3100
}
co.vmosc.com {
    reverse_proxy :2900
}
EOF
    sudo systemctl reload caddy
}

#安装budibase
function DockerBudi(){
    mkdir -p /usr/docker/budi && cd /usr/docker/budi/
    sudo tee /usr/docker/budi/compose.yml <<-'EOF'
services:
  budibase:
    restart: unless-stopped
    image: budibase/budibase:latest
    ports:
      - "3500:80"
    environment:
      JWT_SECRET: localbudibase
      MINIO_ACCESS_KEY: localbudibase
      MINIO_SECRET_KEY: localbudibase
      REDIS_PASSWORD: localbudibase
      COUCHDB_USER: localbudibase
      COUCHDB_PASSWORD: localbudibase
      INTERNAL_API_KEY: localbudibase
    volumes:
      - budibase_data:/data

volumes:
  budibase_data:
    driver: local
EOF
    docker compose up -d
}

function budiCaddy(){
    sudo tee /etc/caddy/Caddyfile <<-'EOF'
{
    email wangtl2001@163.com
}
www.vmosc.com {
    reverse_proxy :2800
}
git.vmosc.com {
    reverse_proxy :3000
}
dr.vmosc.com {
    reverse_proxy :3200
}
il.vmosc.com {
    reverse_proxy :3100
}
co.vmosc.com {
    reverse_proxy :2900
}
bu.vmosc.com {
    reverse_proxy :3500
}
EOF
    sudo systemctl reload caddy
}

#随机16位
function RandSll(){
    DRONE_RPC_SECRET=$(openssl rand -hex 16)
    echo $(DRONE_RPC_SECRET)
}

#Docker安装caddy
function DockerCaddy(){
    mkdir -p /usr/compose/caddy && cd /usr/compose/caddy/
    sudo tee /usr/compose/caddy/compose.yml <<-'EOF'
services:
  caddy:
    image: caddy:<version>
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - ./site:/srv
      - caddy_data:/data
      - caddy_config:/config

volumes:
  caddy_data:
  caddy_config:
EOF

    sudo tee  /usr/compose/caddy/Caddyfile <<-'EOF'
{
    email wangtl2001@163.com
}
www.vmosc.com {
    reverse_proxy :2800
}
git.vmosc.com {
    reverse_proxy :3000
}
dr.vmosc.com {
    reverse_proxy :3200
}
EOF
    cd /usr/compose/caddy/
    docker compose up -d  
}

sudo apt update
if [ $(cat /proc/sys/vm/swappiness) -lt "60" ];then
    SetSwap
fi

if ! [ -x "$(command -v docker)" ];then
until [ -x "$(command -v docker)" ]
do
    InstDocker
done
    ConfDocker

fi

if ! [ -x "$(command -v caddy)" ];then
    InstCaddy
fi

if ! [ -n "$(docker ps -q -f "name=memos")" ]; then
    DockerMemos
    wwwCaddy
fi

if ! [ -n "$(docker ps -q -f "name=gitea")" ]; then
    DockerGitea
    GitCaddy
fi

if ! [ -n "$(docker ps -q -f "name=drone")" ]; then
    DockerDrone
    DrCaddy
fi

#if ! [ -n "$(docker ps -q -f "name=runner")" ]; then
    #DockerRunner
#fi


#DockerCaddy



